package com.uah;

import com.scraper.CourseListingWebData;
import com.scraper.CourseListingWebScraper;
import com.scraper.ScraperType;
import com.scraper.WebReader;

import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.widget.TableLayout;

public class CurrentStudentsPage3 extends Activity {
	TableLayout page3TableLayout;
	TableLayout page3TableLayout2;
	String collegeData;
	ProgressDialog progDialog;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	    setContentView(R.layout.page3);
	    
	    //Start scraper thread
	    BackgroundThread scraperThread = new BackgroundThread();
	    scraperThread.start();

	    if (scraperThread.isAlive())
	    {
	    	progDialog = ProgressDialog.show(this, "Loading",
	    			"....please wait....", true);	
	    }   
		   
	    page3TableLayout = (TableLayout) findViewById(R.id.page3TableLayout);
	    page3TableLayout.removeAllViewsInLayout();
	    
	    // For data legend at bottom of page
	    page3TableLayout2 = (TableLayout) findViewById(R.id.page3TableLayout2);
	    page3TableLayout2.removeAllViewsInLayout();	    
	   
	   try {
		   scraperThread.join();
	    } catch (InterruptedException e) {
	        // ...
	    }
	   
	   // This needs to be done after the scraperThread is done collecting data
	    String[] collegeDataLines = collegeData.split("\\\n");
	    String[] columns = collegeDataLines[2].split("\\ ");
	    int[] columnSizes = new int[columns.length];
	    //Get width of each column
	    for(int x=0;x<columns.length;x++)
	    {
	    	columnSizes[x] = columns[x].length() + 1;
	    }
	    String[] columnData = new String[columns.length];
	    
	    //Parse course listing data into aligned columns
			for(int y=0;y<collegeDataLines.length-2;y++)
			{
				if (collegeDataLines[y].trim().length() > 50)		// Course data
				{
					int seqStart = 0;
					int seqEnd = 0;
					for(int z=0;z<columns.length;z++)
					{
						seqStart = seqEnd;
						seqEnd = seqStart + columnSizes[z];
						if (seqEnd > collegeDataLines[y].length())
						{
							seqEnd = collegeDataLines[y].length()-1;
						}
						columnData[z] = (String) collegeDataLines[y].subSequence(seqStart, seqEnd);
						columnData[z].trim();
					}
					
					DynamicLayoutCreation.createNewRow(columnData, page3TableLayout, this);
				}
				else
				{
					DynamicLayoutCreation.createNewRow(collegeDataLines[y], page3TableLayout2, this);
				}
			}
	    
	}
	
	 public class BackgroundThread extends Thread
	    {
	    	public void run() {  
	    		android.os.Process.setThreadPriority
	    		(android.os.Process.THREAD_PRIORITY_BACKGROUND);
	    		
	    		// Initialize Scraper and Reader (Step 1)
	    		CourseListingWebScraper courseListingScraper = new CourseListingWebScraper();
	    		WebReader webReader = new WebReader(courseListingScraper); 
	    		String collegeCourseListing = null;
	    		
	    		//Get course listing data
	    		Bundle extras = getIntent().getExtras();
	    		if(extras !=null) {
	    			collegeCourseListing = extras.getString("URL");
					courseListingScraper.setScraperType(ScraperType.CollegeCourseListing);
					webReader.scrapeWebPage(collegeCourseListing); 
					CourseListingWebData courseListingData = courseListingScraper.webData; 
					collegeData = courseListingData.courseListingdata; 
	    		}
	    		
	    		if (progDialog.isShowing())
	    			progDialog.dismiss();
	    	}
	    }
	 
	 
}
